package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.entity.GoodAttrs;
import net.zjitc.entity.GoodPics;
import net.zjitc.entity.Goods;
import net.zjitc.mapper.GoodAttrMapper;
import net.zjitc.mapper.GoodMapper;
import net.zjitc.mapper.GoodPicMapper;
import net.zjitc.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IGoodSrvice implements GoodService {
    @Autowired
    private GoodAttrMapper goodAttrMapper;
    @Autowired
    private GoodPicMapper goodpicMapper;
    @Autowired
    private GoodMapper goodMapper;
    @Override
    public Page<Goods> selectPage(Integer pagenum, Integer pagesize) {
        Page<Goods> page=new Page<>(pagenum,pagesize);
        page=goodMapper.selectPage(page,null);
        return page;
    }

    @Override
    public Goods addGood(Goods goods) {
        //添加到good
        goods.setCat_id(1);
        goods.setAdd_time((int)(System.currentTimeMillis()/1000));
        goods.setUpd_time((int)(System.currentTimeMillis()/1000));

        goodMapper.insert(goods);
        Goods goods1 = goodMapper.selectById(goods.getGoods_id());
        //添加到attr
        List<GoodAttrs> attrs = goods.getAttrs();
        for (GoodAttrs attr:attrs) {
            attr.setGoods_id(goods.getGoods_id());
            goodAttrMapper.insert(attr);

        }

        //添加到pic
        List<GoodPics> pics = goods.getPics();
        for (GoodPics pic:pics) {
            pic.setPics_big(pic.getPic());
            pic.setPics_mid(pic.getPic());
            pic.setPics_sma(pic.getPic());
            pic.setGoods_id(goods.getGoods_id());
            goodpicMapper.insert(pic);
        }
        //获取全部
        List<GoodAttrs> attrs1 = findattr(goods.getGoods_id());
        goods1.setAttrs(attrs1);

        List<GoodPics> pics1 = findpic(goods.getGoods_id());
        goods1.setPics(pics1);

        return goods1;


    }

    /**
     * 根据id查询参数
     * @param id
     * @return
     */
    @Override
    public Goods findById(int id) {
        Goods goods1 = goodMapper.selectById(id);
        //查找attr
        List<GoodAttrs> attrs1 = findattr(id);
        goods1.setAttrs(attrs1);
        //查找wrapper
        List<GoodPics> pics1 = findpic(id);
        goods1.setPics(pics1);

        return goods1;
    }

    @Override
    public Goods updateGood(Goods goods, int id) {
        //修改到good
        QueryWrapper<Goods> wrapper=new QueryWrapper<>();
        wrapper.eq("goods_id",id);
        goodMapper.update(goods,wrapper);
        Goods goods1 = goodMapper.selectById(id);
        //添加到attr
        List<GoodAttrs> attrs = goods.getAttrs();
        for (GoodAttrs attr:attrs) {
            QueryWrapper<GoodAttrs> wrapperattr=new QueryWrapper<>();
            wrapperattr.eq("attr_id",attr.getAttr_id()).eq("goods_id",id);
            goodAttrMapper.update(attr,wrapperattr);
        }

        //添加到pic
        List<GoodPics> pics = goods.getPics();
        for (GoodPics pic:pics) {
            pic.setPics_big(pic.getPic());
            pic.setPics_mid(pic.getPic());
            pic.setPics_sma(pic.getPic());
            QueryWrapper<GoodPics> wrapperpic=new QueryWrapper<>();
            wrapperpic.eq("pics_id",pic.getPics_id()).eq("goods_id",id);
            goodpicMapper.update(pic,wrapperpic);
        }
        //获取全部
        List<GoodAttrs> attrs1 = findattr(id);
        goods1.setAttrs(attrs1);

        List<GoodPics> pics1 = findpic(id);
        goods1.setPics(pics1);

        return goods1;
    }

    @Override
    public int deleteGood(int id) {
        int i = goodMapper.deleteById(id);
        return i;
    }

    public List<GoodAttrs> findattr(int id){
        QueryWrapper<GoodAttrs> wrapper=new QueryWrapper<>();
        wrapper.eq("goods_id",id);
        List<GoodAttrs> attrs1 = goodAttrMapper.selectList(wrapper);
        return attrs1;
    }
    public List<GoodPics> findpic(int id){
        QueryWrapper<GoodPics> wrapper1=new QueryWrapper<>();
        wrapper1.eq("goods_id",id);
        List<GoodPics> pics1 = goodpicMapper.selectList(wrapper1);
        return pics1;
    }

}
